﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportService2Ref;
    using WMS6.Web.UserControls;

    public class LoadingRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15721;
        protected GridView gvLoading;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblListTitle;
        protected Label lblMsg;
        protected Label lblTotalCube;
        protected Label lblTotalCubeText;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected Label lblTotalWeight;
        protected Label lblTotalWeightText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalCube;
        private decimal TotalGrossWgt;
        private decimal TotalQty;
        protected UpdatePanel up1;

        public void BindData2StockList(LoadingRptQueryEntity loadingRptQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(loadingRptQueryEntity.CurrentPage);
            DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetLoadingRptDataSet(loadingRptQueryEntity);
            base.BindData2GridView(this.gvLoading, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            LoadingRptQueryEntity loadingRptQueryEntity = this.GetLoadingRptQueryEntity();
            loadingRptQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = loadingRptQueryEntity.CurrentPage;
            loadingRptQueryEntity.CurrentPage = "0";
            try
            {
                using (DataSet ds = AnalyticsReportService2ClientProxy.GetInstance().GetLoadingRptDataSet(loadingRptQueryEntity))
                {
                    base.Export2Excel("LoadingRpt.xls", "15721", this.gvLoading, ds);
                }
            }
            finally
            {
                loadingRptQueryEntity.IsGetAll = false;
                loadingRptQueryEntity.CurrentPage = currentPageIndex;
                this.SetLoadingRptQueryEntity(loadingRptQueryEntity);
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            LoadingRptQueryEntity loadingRptQueryEntity = this.GetLoadingRptQueryEntity();
            loadingRptQueryEntity.CurrentPage = "0";
            this.BindData2StockList(loadingRptQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            LoadingRptQueryEntity loadingRptQueryEntity = this.GetLoadingRptQueryEntity();
            loadingRptQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2StockList(loadingRptQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            LoadingRptQueryEntity loadingRptQueryEntity = this.GetLoadingRptQueryEntity();
            loadingRptQueryEntity.BolID = "";
            loadingRptQueryEntity.ExternOrderId2 = "";
            loadingRptQueryEntity.OwnerID = "";
            loadingRptQueryEntity.CarrierID = "";
            loadingRptQueryEntity.Status = "";
            loadingRptQueryEntity.Paid = "";
            loadingRptQueryEntity.BeginDate = "";
            loadingRptQueryEntity.EndDate = "";
            this.BindData2StockList(loadingRptQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            LoadingRptQueryEntity loadingRptQueryEntity = this.GetLoadingRptQueryEntity();
            if (int.Parse(loadingRptQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                loadingRptQueryEntity.CurrentPage = (int.Parse(loadingRptQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2StockList(loadingRptQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            LoadingRptQueryEntity loadingRptQueryEntity = this.GetLoadingRptQueryEntity();
            if (int.Parse(loadingRptQueryEntity.CurrentPage) > 0)
            {
                loadingRptQueryEntity.CurrentPage = (int.Parse(loadingRptQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2StockList(loadingRptQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadingRptQueryEntity loadingRptQueryEntity = this.GetLoadingRptQueryEntity();
            this.BindData2StockList(loadingRptQueryEntity);
        }

        private LoadingRptQueryEntity GetLoadingRptQueryEntity()
        {
            return (base.PageQueryEntitySession as LoadingRptQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                LoadingRptQueryEntity loadingRptQueryEntity = this.GetLoadingRptQueryEntity();
                loadingRptQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2StockList(loadingRptQueryEntity);
            }
        }

        protected void gvLoading_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F15721", this.gvLoading);
        }

        protected void gvLoading_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (this.hdnActionID.Value != "EXPORT")
                {
                    e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                    e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                    ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                    e.Row.Attributes.Add("ondblclick", "JavaScript:document.getElementById('" + ((CheckBox) e.Row.FindControl("chbSelect")).ClientID + "').click();");
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                    if (((Label) e.Row.FindControl("lblQTY")) != null)
                    {
                        this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQTY")).Text != "") ? ((Label) e.Row.FindControl("lblQTY")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblGROSS_WEIGHT")) != null)
                    {
                        this.TotalGrossWgt += decimal.Parse((((Label) e.Row.FindControl("lblGROSS_WEIGHT")).Text != "") ? ((Label) e.Row.FindControl("lblGROSS_WEIGHT")).Text : "0");
                    }
                    if (((Label) e.Row.FindControl("lblCUBE")) != null)
                    {
                        this.TotalCube += decimal.Parse((((Label) e.Row.FindControl("lblCUBE")).Text != "") ? ((Label) e.Row.FindControl("lblCUBE")).Text : "0");
                    }
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
                this.lblTotalWeightText.Text = base.GetPreferenceDigit(this.TotalGrossWgt);
                this.lblTotalCubeText.Text = base.GetPreferenceDigit(this.TotalCube);
                if (this.hdnActionID.Value == "EXPORT")
                {
                    for (int i = 0; i < this.gvLoading.Columns.Count; i++)
                    {
                        if (this.gvLoading.Columns[i].Visible)
                        {
                            this.gvLoading.Columns[i].ItemStyle.Reset();
                            this.gvLoading.Columns[i].ControlStyle.Reset();
                        }
                    }
                }
            }
        }

        protected void gvLoading_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                LoadingRptQueryEntity loadingRptQueryEntity = this.GetLoadingRptQueryEntity();
                loadingRptQueryEntity.SortField = e.SortExpression;
                if ((loadingRptQueryEntity.SortDirection == "ASC") || (loadingRptQueryEntity.SortDirection == ""))
                {
                    loadingRptQueryEntity.SortDirection = "DESC";
                }
                else if (loadingRptQueryEntity.SortDirection == "DESC")
                {
                    loadingRptQueryEntity.SortDirection = "ASC";
                }
                this.BindData2StockList(loadingRptQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            LoadingRptQueryEntity loadingRptQueryEntity = new LoadingRptQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    loadingRptQueryEntity.BolID = this.PreviousPage.BolID;
                    loadingRptQueryEntity.ExternOrderId2 = this.PreviousPage.ExternOrderID2;
                    loadingRptQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    loadingRptQueryEntity.CarrierID = this.PreviousPage.CarrierID;
                    loadingRptQueryEntity.Status = this.PreviousPage.Status;
                    loadingRptQueryEntity.Paid = this.PreviousPage.Paid;
                    loadingRptQueryEntity.BeginDate = this.PreviousPage.BeginDate;
                    loadingRptQueryEntity.EndDate = this.PreviousPage.EndDate;
                }
                else
                {
                    loadingRptQueryEntity.BolID = "";
                    loadingRptQueryEntity.ExternOrderId2 = "";
                    loadingRptQueryEntity.OwnerID = "";
                    loadingRptQueryEntity.CarrierID = "";
                    loadingRptQueryEntity.Status = "";
                    loadingRptQueryEntity.Paid = "";
                    loadingRptQueryEntity.BeginDate = "";
                    loadingRptQueryEntity.EndDate = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            loadingRptQueryEntity.IsGetAll = false;
            loadingRptQueryEntity.CurrentPage = "0";
            loadingRptQueryEntity.PageSize = this.gvLoading.PageSize.ToString();
            loadingRptQueryEntity.SortField = "CARRIER,CREATED_DATE,BOL_ID";
            loadingRptQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvLoading.PageSize;
            this.SetLoadingRptQueryEntity(loadingRptQueryEntity);
            this.BindData2StockList(loadingRptQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.InitPage(this.Page);
            base.InitAnalyticsReportGridview(this.gvLoading);
            this.LoadData();
        }

        public void SetLoadingRptQueryEntity(LoadingRptQueryEntity loadingRptQueryEntity)
        {
            base.PageQueryEntitySession = loadingRptQueryEntity;
        }

        public LoadingRptQuery PreviousPage
        {
            get
            {
                return (LoadingRptQuery) base.PreviousPage;
            }
        }
    }
}

